# -*- coding: utf-8 -*-
'''
    :file: extensions.py
    :author: -Farmer
    :url: https://blog.farmer233.top
    :date: 2021/02/10 16:16:37
'''
# from flask import
from flask.globals import current_app, g
from flask_sqlalchemy import SQLAlchemy
from flask_httpauth import HTTPTokenAuth
from flask_mail import Mail
from flask_cors import CORS
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
# from flask_restful import Api
from flask_apscheduler import APScheduler
from flask_marshmallow import Marshmallow

# 数据库ORM框架
db = SQLAlchemy()
# 邮件发送
mail = Mail()
# token插件
auth = HTTPTokenAuth(scheme='Bearer')
# 跨域插件
cors = CORS()
# 定时任务插件
scheduler = APScheduler()
# 序列化，反序列化插件
marshmallow = Marshmallow()

# 初始化token鉴权插件
@auth.verify_token
def verify_token(token):
    g.user = None
    g.username = None
    try:
        data = Serializer(current_app.config['SECRET_KEY']).loads(token)
    except Exception as e:
        return False
    if "id" in data:
        g.user = data["id"]
        g.username = data['username']
        g.power = data['power']
        return True
    return False

